%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Rand_Vect.m
% M-Function written for MATLAB 5.0
%
% Chris Bartels
% 10/30/01
% Stat. 516
% University of Washington
%
% This function generates multinomial random vectors from a probability vector.
%
% Input:
%   p_vctr - Probability vector
%   smpl_sz - Sample size, number entries in output vector
%
% Output:
%   V - Random vector of length smpl_sz
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [V] = Rand_Vect(p_vctr, smpl_sz)

%
% Make sure the entries in the probability vector add up to 1
%
if ( sum(p_vctr) ~= 1)
   error('Rand_Vect:  Given probability vector is not valid')
end

%
% Make a vector that will be used to map a decimal from 0-1 to the states 
%  in p_vctr
%
lp_vctr(1) = p_vctr(1);
for i = 2:length(p_vctr)
   lp_vctr(i) = lp_vctr(i-1) + p_vctr(i);
end

%
% Create output vector
%
for smpl = 1:smpl_sz
   
   x = rand(1);
   for i = 1:length(lp_vctr)
      if (x < lp_vctr(i))
         V(smpl) = i;
         break;
      end
   end
   
end

